package com.hjj.services;

import com.hjj.beans.Qa;
import com.hjj.beans.Question;
import com.hjj.mapper.QaMapper;
import com.hjj.mapper.QuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

@Service
public class QaSercicesImp implements QaServices{
    @Autowired
    QaMapper qaMapper;

    @Autowired
    QuestionMapper questionMapper;

    @Override
    public ArrayList<Qa> getAll() {
        return qaMapper.getAll();
    }

    @Override
    public Qa get(int id) {
        return qaMapper.get(id);
    }

    @Override
    @Transactional
    public boolean add(Qa qa) {
        Question question = qa.getQuestion();

        int questionAdd = questionMapper.add(question);

        qa.setQuestion_id(question.getId());

        int qaAdd = qaMapper.add(qa);

        return (questionAdd > 0 && qaAdd > 0) ? true : false;
    }

    @Override
    @Transactional
    public boolean edit(Qa qa) {
        Question question = qa.getQuestion();

        System.out.println("修改的数据：" + qa);

        int questionEdit = questionMapper.edit(question);

        int qaEdit = qaMapper.edit(qa);

        return (questionEdit > 0 && qaEdit > 0) ? true : false;
    }

    @Override
    public boolean del(int id) {
        return questionMapper.del(id) > 0 ? true : false;
    }
}
